package com.dykj.fireFighting.web.base.upRepairRecord;

import java.util.*;

import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.file.entity.DataFiles;
import com.dykj.fireFighting.api.base.file.service.DataFilesService;
import com.dykj.fireFighting.api.base.maintenance.dto.UpRepairRecordDto;
import com.dykj.fireFighting.api.base.maintenance.entity.UpRepairRecord;
import com.dykj.fireFighting.api.base.maintenance.service.IUpRepairRecordService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;



/**
 * 上传维修记录表（单独）接口
 *
 * @Author JacksonTu
 * @Date  2021-12-27 08:49:56
 */
@Slf4j
@Api(value = "上传维修记录表（单独）接口", tags = {"上传维修记录表（单独）接口"})
@RestController
@RequestMapping("upRepairRecord")
public class UpRepairRecordController {

    @DubboReference
    private IUpRepairRecordService upRepairRecordService;
    @DubboReference
    private IEnterpriseService enterpriseService;
    @DubboReference
    private DataFilesService filesService;

    /**
     * 上传维修记录表（单独）列表
     *
     * @return
     */
    @AutoLog(value = "上传维修记录表（单独）-分页列表查询")
    @ApiOperation(value = "上传维修记录表（单独）分页列表", notes = "上传维修记录表（单独）分页列表")
    @GetMapping("/list")
    @RequiresPermissions("upRepairRecord/list")
    public CommonResult<PageVo<UpRepairRecord>> treeGrid(UpRepairRecordDto upRepairRecordDto) {
        LoginUserVo user = ShiroUtil.getUser();
        /*社会单位:0，维保单位:1，监控中心:2*/
        if(user.getEnterpriseType().equals(0)){
            upRepairRecordDto.setSocialUnitsCode(user.getEnterpriseId().toString());
        }
        if(user.getEnterpriseType().equals(1)){
            upRepairRecordDto.setWeibaoUnitsCode(user.getEnterpriseId().toString());
        }
        PageVo<UpRepairRecord> page = upRepairRecordService.pageList(upRepairRecordDto);
        return CommonResult.success(page);
    }

    /**
     * 获取上传维修记录表（单独）信息
     */
    @AutoLog(value = "上传维修记录表（单独）-获取上传维修记录表（单独）信息")
    @ApiOperation(value = "获取上传维修记录表（单独）信息", notes = "获取上传维修记录表（单独）信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("upRepairRecord/info")
    public CommonResult<UpRepairRecord> info(@PathVariable("id") String id){
        UpRepairRecord upRepairRecord = upRepairRecordService.getInfoById(id);
        if(null !=upRepairRecord.getPicturePath() && StringUtils.isNotBlank(upRepairRecord.getPicturePath())){
            String[] split = upRepairRecord.getPicturePath().split(",");
            List<String> ids = Arrays.asList(split);
            List<DataFiles> files=filesService.getDataFilesByIds(ids);
            upRepairRecord.setFiles(files);
        }
        return CommonResult.success(upRepairRecord);
    }

    /**
     * 保存上传维修记录表（单独）信息
     */
    @AutoLog(value = "上传维修记录表（单独）-保存上传维修记录表（单独）信息")
    @ApiOperation(value = "保存上传维修记录表（单独）信息", notes = "保存上传维修记录表（单独）信息")
    @PostMapping("/save")
    @RequiresPermissions("upRepairRecord/save")
    public CommonResult save(@Valid @RequestBody UpRepairRecord upRepairRecord) {
        try {
            LoginUserVo user = ShiroUtil.getUser();
            upRepairRecord.setCreateDate(new Date());
            upRepairRecord.setCreateBy(user.getId().toString());
            upRepairRecord.setWeibaoUnitsCode(user.getEnterpriseId().toString());
            if (null !=upRepairRecord.getSocialUnitsCode() && StringUtils.isNotBlank(upRepairRecord.getSocialUnitsCode())){
                Enterprise enterprise = enterpriseService.getById(upRepairRecord.getSocialUnitsCode());
                upRepairRecord.setSocialUnitsName(enterprise.getEnterpriseName());
            }
                upRepairRecordService.save(upRepairRecord);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改上传维修记录表（单独）信息
     */
    @AutoLog(value = "上传维修记录表（单独）-修改上传维修记录表（单独）信息")
    @ApiOperation(value = "修改上传维修记录表（单独）信息", notes = "修改上传维修记录表（单独）信息")
    @PostMapping("/update")
    @RequiresPermissions("upRepairRecord/update")
    public CommonResult update(@Valid @RequestBody UpRepairRecord upRepairRecord) {
        try {

                upRepairRecordService.updateById(upRepairRecord);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除上传维修记录表（单独）信息
     */
    @AutoLog(value = "上传维修记录表（单独）-删除上传维修记录表（单独）信息")
    @ApiOperation(value = "删除上传维修记录表（单独）信息", notes = "删除上传维修记录表（单独）信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("upRepairRecord/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                /*upRepairRecordService.removeByIds(Arrays.asList(ids));*/
            upRepairRecordService.deleteDel(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
